package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import com.simon.annual.entity.ReportNeeq;
import com.simon.annual.entity.ReportRisk;
import com.simon.annual.repositoy.IReportRiskRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;

/**
 * 第一节：处理声明与提示
 * */
@Service
public class DeclarationPromptImpl{
	
	@Resource
	private ICompanyPublicInfo cmpanyPublicInfo;
	@Resource
	private IReportRiskRepositoy reportRiskRepositoy;
	

	public ReportNeeq analysis(Element na, ReportNeeq reportNeeq,int companyPtrId) {
//		("\u4E8B\u9879");
		if(na.parent().tagName().equals("tr")){
			if(na.parent().select("table").size()>0){
				na= na.parent().select("table").first();
			}else{
				na = na.parent().parent().parent();
			}
		}
		Element newe = cmpanyPublicInfo.findNextTag("table",na);
		if(newe.select("table tr").last().children().size()<=1){
			newe = cmpanyPublicInfo.findNextTag("table", newe);
		}
		Elements tds = newe.select("tbody").last().select("tr > *");
		reportNeeq.setHasManagementDisagreement(tds.get(3).text());
		reportNeeq.setHasUnattendedPrinciple(tds.get(5).text());
		reportNeeq.setHasDisclosureExemption(tds.get(7).text());
		//处理选填项
		/*int index =1;
		Elements trs = nextTable.nextElementSibling().select("table tr");
		if(trs.size()>0&&trs.get(0).select("td").size()==1){
			index = index+1;
			reportNeeq.setManagementDisagreementReview(na.siblingElements().get(na.elementSiblingIndex()+index).text());
			Element  hasnext = na.siblingElements().get(na.elementSiblingIndex()+index+1);
			if(hasnext.select("table tr td").size()==1){
				index = index+1;
				reportNeeq.setUnattendedPrincipleReview(na.siblingElements().get(na.elementSiblingIndex()+index).text());
			}
			hasnext = na.siblingElements().get(na.elementSiblingIndex()+index+1);
			if(hasnext.select("table tr td").size()==1){
				index = index+1;
				reportNeeq.setUnattendedPrincipleReview(na.siblingElements().get(na.elementSiblingIndex()+index).text());
			}
		}*/
		//重要风险提示
		List<ReportRisk> riskList = new ArrayList<ReportRisk>();
		Element table = cmpanyPublicInfo.buildTable(cmpanyPublicInfo.findNextTag("table",newe));
		Elements tr = table.select("table tr");
		for (int i = 1; i < tr.size()-1; i++) {
			ReportRisk risk = new ReportRisk();
			Elements td = tr.get(i).children();
			risk.setName(td.get(0).text());
			risk.setDescription(td.get(1).text());
			risk.setProjectScopeId(companyPtrId);
			riskList.add(risk);
		}
		reportNeeq.setIsMajorRiskChanged(table.select("table tr > * ").last().text());
		reportRiskRepositoy.save(riskList);
		return reportNeeq;
	}


}
